/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * A class representing creation dialog on screen.
 * For example:
 * <pre>
 *    ElmLiberationsDialog dgl = new ElmLiberationsDialog(parent);
 *    dgl.pack();
 * </pre>
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public final class ElmLiberationsDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private FemModel fmodel;
	
	private String anlType;
	
	private JCheckBox liberationLdx1 = new JCheckBox("");
	private JCheckBox liberationLdy1 = new JCheckBox("");
	private JCheckBox liberationLdz1 = new JCheckBox("");
	private JCheckBox liberationLrx1 = new JCheckBox("");
	private JCheckBox liberationLry1 = new JCheckBox("");
	private JCheckBox liberationLrz1 = new JCheckBox("");
	private JCheckBox liberationLdx2 = new JCheckBox("");
	private JCheckBox liberationLdy2 = new JCheckBox("");
	private JCheckBox liberationLdz2 = new JCheckBox("");
	private JCheckBox liberationLrx2 = new JCheckBox("");
	private JCheckBox liberationLry2 = new JCheckBox("");
	private JCheckBox liberationLrz2 = new JCheckBox("");
	public boolean Ldx1;
	public boolean Ldy1;
	public boolean Ldz1;
	public boolean Lrx1;
	public boolean Lry1;
	public boolean Lrz1;
	public boolean Ldx2;
	public boolean Ldy2;
	public boolean Ldz2;
	public boolean Lrx2;
	public boolean Lry2;
	public boolean Lrz2;
	
//*****************************************************************************
	
	/**
	 * Constructor method, calling super with the especific title.
	 *
	 * @param parent The parent of the class.
	 * @see gui.InsaneInternalInterface
	 */
	public ElmLiberationsDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		fmodel = parent.getModel().getDiscreteModel();
		
		setUpFormat();
		setUpFields();
		
//		Sets up the liberations panel
		JPanel liberationsPane = new JPanel();
		liberationsPane.setPreferredSize(new Dimension(360,300));
		liberationsPane.setLayout(new BoxLayout(liberationsPane, BoxLayout.Y_AXIS));
		
		JPanel libPicPane = new JPanel();
		libPicPane.setMaximumSize(new Dimension(500,100));
		libPicPane.add(new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/Liberation100.gif")) ));
		liberationsPane.add(libPicPane);
		
		JPanel lib1Pane = new JPanel();
		lib1Pane.setLayout(new GridLayout(2,3,2,2));
		lib1Pane.setMaximumSize(new Dimension(500,90));
		lib1Pane.setBorder(BorderFactory.createCompoundBorder(
			BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmElaConDialog01")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		
		JPanel libD1xPane = new JPanel();
		JPanel libD1yPane = new JPanel();
		JPanel libD1zPane = new JPanel();
		libD1xPane.add(new JLabel("Dx:"));
		libD1xPane.add(liberationLdx1);
		libD1yPane.add(new JLabel("Dy:"));
		libD1yPane.add(liberationLdy1);
		libD1zPane.add(new JLabel("Dz:"));
		libD1zPane.add(liberationLdz1);
		lib1Pane.add(libD1xPane);
		lib1Pane.add(libD1yPane);
		lib1Pane.add(libD1zPane);
		
		JPanel libR1xPane = new JPanel();
		JPanel libR1yPane = new JPanel();
		JPanel libR1zPane = new JPanel();
		libR1xPane.add(new JLabel("Rx:"));
		libR1xPane.add(liberationLrx1);
		libR1yPane.add(new JLabel("Ry:"));
		libR1yPane.add(liberationLry1);
		libR1zPane.add(new JLabel("Rz:"));
		libR1zPane.add(liberationLrz1);
		lib1Pane.add(libR1xPane);
		lib1Pane.add(libR1yPane);
		lib1Pane.add(libR1zPane);
		liberationsPane.add(lib1Pane);
		
		JPanel lib2Pane = new JPanel();
		lib2Pane.setLayout(new GridLayout(2,3,2,2));
		lib2Pane.setMaximumSize(new Dimension(500,90));
        lib2Pane.setBorder(BorderFactory.createCompoundBorder(
        	BorderFactory.createTitledBorder(parent.getResourceBundle().getString("dElmElaConDialog02")),
			BorderFactory.createEmptyBorder(2,2,2,2)));
		
		JPanel libD2xPane = new JPanel();
		JPanel libD2yPane = new JPanel();
		JPanel libD2zPane = new JPanel();
		libD2xPane.add(new JLabel("Dx:"));
		libD2xPane.add(liberationLdx2);
		libD2yPane.add(new JLabel("Dy:"));
		libD2yPane.add(liberationLdy2);
		libD2zPane.add(new JLabel("Dz:"));
		libD2zPane.add(liberationLdz2);
		lib2Pane.add(libD2xPane);
		lib2Pane.add(libD2yPane);
		lib2Pane.add(libD2zPane);
		
		JPanel libR2xPane = new JPanel();
		JPanel libR2yPane = new JPanel();
		JPanel libR2zPane = new JPanel();
		libR2xPane.add(new JLabel("Rx:"));
		libR2xPane.add(liberationLrx2);
		libR2yPane.add(new JLabel("Ry:"));
		libR2yPane.add(liberationLry2);
		libR2zPane.add(new JLabel("Rz:"));
		libR2zPane.add(liberationLrz2);
		lib2Pane.add(libR2xPane);
		lib2Pane.add(libR2yPane);
		lib2Pane.add(libR2zPane);
		liberationsPane.add(lib2Pane);
		
		setPane(liberationsPane);
		
    }
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(parent, parent.getResourceBundle().getString("dElmElaConDialog05"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		
		try {
			
			Ldx1 = liberationLdx1.isSelected();
			Ldy1 = liberationLdy1.isSelected();
			Ldz1 = liberationLdz1.isSelected();
			Lrx1 = liberationLrx1.isSelected();
			Lry1 = liberationLry1.isSelected();
			Lrz1 = liberationLrz1.isSelected();
			Ldx2 = liberationLdx2.isSelected();
			Ldy2 = liberationLdy2.isSelected();
			Ldz2 = liberationLdz2.isSelected();
			Lrx2 = liberationLrx2.isSelected();
			Lry2 = liberationLry2.isSelected();
			Lrz2 = liberationLrz2.isSelected();
			
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		
		if (this.parent.getController().getSelection().size() == 1 ) {
			
			FrameElement elm = (FrameElement)this.parent.getController().getSelection().get(0);
			
			//Setting liberations fields
			liberationLdx1.setSelected(elm.getLiberation(0));
			liberationLdy1.setSelected(elm.getLiberation(1));
			liberationLdz1.setSelected(elm.getLiberation(2));
			liberationLrx1.setSelected(elm.getLiberation(3));
			liberationLry1.setSelected(elm.getLiberation(4));
			liberationLrz1.setSelected(elm.getLiberation(5));
			liberationLdx2.setSelected(elm.getLiberation(6));
			liberationLdy2.setSelected(elm.getLiberation(7));
			liberationLdz2.setSelected(elm.getLiberation(8));
			liberationLrx2.setSelected(elm.getLiberation(9));
			liberationLry2.setSelected(elm.getLiberation(10));
			liberationLrz2.setSelected(elm.getLiberation(11));
			
		} else {
			
			liberationLdx1.setSelected(false);
			liberationLdy1.setSelected(false);
			liberationLdz1.setSelected(false);
			liberationLrx1.setSelected(false);
			liberationLry1.setSelected(false);
			liberationLrz1.setSelected(false);
			liberationLdx2.setSelected(false);
			liberationLdy2.setSelected(false);
			liberationLdz2.setSelected(false);
			liberationLrx2.setSelected(false);
			liberationLry2.setSelected(false);
			liberationLrz2.setSelected(false);
			
		}
		
		//Enabling or disabling fields according to analysis model type
		if (this.parent.getController().getSelection().size() == 1)
			anlType = ((FrameElement)this.parent.getController().getSelection().get(0)).getAnalysisModel().getType();
		else anlType = fmodel.getGlobalAnalysisModel().getType();
		
		if (anlType.equals("Beam")) {
			
			liberationLdx1.setEnabled(false);
			liberationLdx2.setEnabled(false);
			liberationLdy1.setEnabled(true);
			liberationLdy2.setEnabled(true);
			liberationLdz1.setEnabled(false);
			liberationLdz2.setEnabled(false);
			liberationLrx1.setEnabled(false);
			liberationLrx2.setEnabled(false);
			liberationLry1.setEnabled(false);
			liberationLry2.setEnabled(false);
			liberationLrz1.setEnabled(true);
			liberationLrz2.setEnabled(true);
			
		} else if (anlType.equals("PlaneFrame")) {
			
			liberationLdx1.setEnabled(true);
			liberationLdx2.setEnabled(true);
			liberationLdy1.setEnabled(true);
			liberationLdy2.setEnabled(true);
			liberationLdz1.setEnabled(false);
			liberationLdz2.setEnabled(false);
			liberationLrx1.setEnabled(false);
			liberationLrx2.setEnabled(false);
			liberationLry1.setEnabled(false);
			liberationLry2.setEnabled(false);
			liberationLrz1.setEnabled(true);
			liberationLrz2.setEnabled(true);
			
		} else if (anlType.equals("PlaneTruss")) {
			
			liberationLdx1.setEnabled(true);
			liberationLdx2.setEnabled(true);
			liberationLdy1.setEnabled(true);
			liberationLdy2.setEnabled(true);
			liberationLdz1.setEnabled(false);
			liberationLdz2.setEnabled(false);
			liberationLrx1.setEnabled(false);
			liberationLrx2.setEnabled(false);
			liberationLry1.setEnabled(false);
			liberationLry2.setEnabled(false);
			liberationLrz1.setEnabled(false);
			liberationLrz2.setEnabled(false);
			
		} else if (anlType.equals("Grid")) {
			
			liberationLdx1.setEnabled(false);
			liberationLdx2.setEnabled(false);
			liberationLdy1.setEnabled(true);
			liberationLdy2.setEnabled(true);
			liberationLdz1.setEnabled(false);
			liberationLdz2.setEnabled(false);
			liberationLrx1.setEnabled(true);
			liberationLrx2.setEnabled(true);
			liberationLry1.setEnabled(false);
			liberationLry2.setEnabled(false);
			liberationLrz1.setEnabled(true);
			liberationLrz2.setEnabled(true);
			
		}
		
	}
	
//*****************************************************************************
}

